<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

</head>

<body>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <!-- 引入组件库 -->
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <div id="app">
    下班时间设置 <el-date-picker v-model="endDate" type="datetime" placeholder="选择日期时间" style="margin: 20px 0 0;">
    </el-date-picker>
    {{title}} <el-date-picker v-model="yearDate" type="datetime" placeholder="选择日期时间" style="margin: 20px 0 0;">
    </el-date-picker>
    <el-button type="primary" @click="handlesure" style="margin: 20px 0 0;">一键下班</el-button>
    <p :style="style1">距下班还有 {{time}}</p>
    <p :style="style2">距过年还有 {{time1}}</p>
    <p :style="style3">当前时间 {{currentTime}}</p>
  </div>
  <script>
    var app = new Vue({
      el: '#app',
      data: {
        title: localStorage.getItem('title') || '',
        time: localStorage.getItem('time') || '',
        time1: localStorage.getItem('time1') || '',
        currentTime: localStorage.getItem('currentTime') || '',
        yearDate: localStorage.getItem('yearDate') || '',
        endDate: localStorage.getItem('endDate') || '',
        style1: { color: 'green' },
        style2: { color: 'blue' },
        style3: { color: 'red' },
      },
      watch: {
        endDate () {
          localStorage.setItem('endDate', this.endDate)
        },
        yearDate () {
          localStorage.setItem('yearDate', this.yearDate)
        }
      },
      created () { },
      mounted () {
        setInterval(() => {
          let nowtime = new Date()  //获取当前时间
          let endtime = new Date(localStorage.getItem('endDate'))  //定义结束时间
          let lefttime = endtime.getTime() - nowtime.getTime()  //距离结束时间的毫秒数
          let leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24)) //计算天数
          let lefth = Math.floor(lefttime / (1000 * 60 * 60) % 24)  //计算小时数
          let leftm = Math.floor(lefttime / (1000 * 60) % 60)  //计算分钟数
          lefts = Math.floor(lefttime / 1000 % 60)  //计算秒数
          leftd = leftd < 10 ? '0' + leftd : leftd
          lefth = lefth < 10 ? '0' + lefth : lefth
          leftm = leftm < 10 ? '0' + leftm : leftm
          lefts = lefts < 10 ? '0' + lefts : lefts
          this.time = leftd + "天" + lefth + "小时" + leftm + "分" + lefts + '秒';  //返回倒计时的字符串
          localStorage.setItem('time', this.time)
          // this.style1.color = `rgb(${parseInt(Math.random() * 255)}, ${parseInt(Math.random() * 255)}, ${parseInt(Math.random() * 255)})`
          if (lefth === '00' && leftm === "00") {
            window.open('https://www.baidu.com/s?wd=%E4%B8%8B%E7%8F%AD%E4%BA%86+%E5%90%8C%E5%AD%A6+%E8%AE%B0%E5%BE%97%E6%89%93%E5%8D%A1')
          }
        }, 1000)

        setInterval(() => {
          let nowtime = new Date()  //获取当前时间
          let endtime = new Date(localStorage.getItem('yearDate'))  //定义结束时间
          let lefttime = endtime.getTime() - nowtime.getTime()  //距离结束时间的毫秒数
          let leftd = Math.floor(lefttime / (1000 * 60 * 60 * 24)) //计算天数
          let lefth = Math.floor(lefttime / (1000 * 60 * 60) % 24)  //计算小时数
          let leftm = Math.floor(lefttime / (1000 * 60) % 60)  //计算分钟数
          lefts = Math.floor(lefttime / 1000 % 60)  //计算秒数
          leftd = leftd < 10 ? '0' + leftd : leftd
          lefth = lefth < 10 ? '0' + lefth : lefth
          leftm = leftm < 10 ? '0' + leftm : leftm
          lefts = lefts < 10 ? '0' + lefts : lefts
          this.time1 = leftd + "天" + lefth + "小时" + leftm + "分" + lefts + '秒';  //返回倒计时的字符串
          localStorage.setItem('time1', this.time1)
          // this.style2.color = `rgb(${parseInt(Math.random() * 255)}, ${parseInt(Math.random() * 255)}, ${parseInt(Math.random() * 255)})`
        }, 1000)


        setInterval(() => {
          let dat = new Date()
          var year = dat.getFullYear();
          var mon = (dat.getMonth() + 1) < 10 ? "0" + (dat.getMonth() + 1) : dat.getMonth() + 1;
          var data = dat.getDate() < 10 ? "0" + (dat.getDate()) : dat.getDate();
          var hour = dat.getHours() < 10 ? "0" + (dat.getHours()) : dat.getHours();
          var min = dat.getMinutes() < 10 ? "0" + (dat.getMinutes()) : dat.getMinutes();
          var seon = dat.getSeconds() < 10 ? "0" + (dat.getSeconds()) : dat.getSeconds();
          this.currentTime = year + "年" + mon + "月" + data + "日" + hour + "时" + min + "分" + seon + '秒';
          localStorage.setItem('currentTime', this.currentTime)
          // this.style3.color = `rgb(${parseInt(Math.random() * 255)}, ${parseInt(Math.random() * 255)}, ${parseInt(Math.random() * 255)})`


        }, 1000)


      },
      methods: {
        handlesure () {
          // setTimeout(() => {
          window.open('https://www.baidu.com/s?wd=%E4%B8%8B%E7%8F%AD%E4%BA%86+%E5%90%8C%E5%AD%A6+%E8%AE%B0%E5%BE%97%E6%89%93%E5%8D%A1')
          // }, 2000)
        },


      }
    })
  </script>
</body>

</html>